<?php

namespace App\Http\Controllers\Admin\hwtc;

use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Admin\CommonController;
use App\Models\hwtc\Downfile,App\Models\hwtc\Otatype;
use DB;


class FileController extends CommonController
{

    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_file' => 'active',
            '_downfile' => 'active',
        ]);
    }

    public function index(Request $request)
    {
        //查找
        $where = function ($query) use ($request) {
            if ($request->has('keyword') and $request->keyword != '') {
                $search = "%" . $request->keyword . "%";
                $query->where('name', 'like', $search);
            }
        };

        $downfiles=Downfile::where($where)->orderBy('created_at','desc')->paginate(config('hwtc.page_size'));
//        return $downfiles;
        return view('admin.hwtc.downfile.index')->with('downfiles',$downfiles);
    }


    //新增
    public function create()
    {
        return view('admin.hwtc.downfile.create');
    }

    //存储
    public function store(Request $request)
    {
//        return $request->all();
        $this->validate($request, [
            'name' => 'required|max:255',
        ]);
        $data=$request->all();
        if(substr($data['url'],0,4)=='http'){
            //说明是外部链接！

            try{
                $info=json_decode(file_get_contents($data['url']."?qhash/md5"),true);

                $data['filesize']=$info['fsize'];
                $data['md5']=$info['hash'];

            }catch (\Exception $e){
//                return 'url异常！';
                $data['filesize']=0;
                $data['md5']=0;
            }
        }else{
            //上传到本地的url
            if(file_exists(getcwd().$data['url']) && $data['url']!=''){
                $data['filesize']=filesize(getcwd().$data['url']);
                $data['md5']=md5File($data['url']);
            }else{
                $data['filesize']=0;
                $data['md5']=0;
            }
        }
        Downfile::create($data); //把所有提交上来的数据添加到数据库
        return redirect(route('hwtc.downfile.index'))->with('success','新增downfile成功');
    }

    //编辑
    public function edit($id)
    {
        $downfile=Downfile::find($id);
        return view('admin.hwtc.downfile.edit')->with('downfile',$downfile);
    }

    //更新
    public function update(Request $request,$id)
    {
//        return $request->all();
        $this->validate($request, [
            'name' => 'required|max:255',
        ]);
        $data=$request->all();
        if(substr($data['url'],0,4)=='http'){
            //说明是外部链接！

            try{
                $info=json_decode(file_get_contents($data['url']."?qhash/md5"),true);

                $data['filesize']=$info['fsize'];
                $data['md5']=$info['hash'];

            }catch (\Exception $e){
//                return 'url异常！';
                $data['filesize']=0;
                $data['md5']=0;
            }
        }else{
            //上传到本地的url
            if(file_exists(getcwd().$data['url']) && $data['url']!=''){
                $data['filesize']=filesize(getcwd().$data['url']);
                $data['md5']=md5File($data['url']);
            }else{
                $data['filesize']=0;
                $data['md5']=0;
            }
        }
        $downfile=Downfile::find($id);
        $downfile->update($data); //修改request上提交的所有数据
        return redirect(route('hwtc.downfile.index'))->with('success','修改downfile成功!');
    }

    //删除
    public function destroy($id)
    {

        $filepath=Downfile::select('url')->find($id);
        if(file_exists(getcwd().$filepath->url)){
            @unlink(getcwd().$filepath->url);
        }
        Downfile::destroy($id);
        return redirect(route('hwtc.downfile.index'))->with('success','删除downfile成功');
    }
    //多选删除
    public function destroy_checked(Request $request)
    {
        $ids=$request->input("checked_id"); //$ids是个数组
        foreach ($ids as $id){
            $filepath=Downfile::select('url')->find($id);
            if(file_exists(getcwd().$filepath->url)){
                @unlink(getcwd().$filepath->url);
            }
            Downfile::destroy($id);
        }
    }

    //downfile download
    public function filedownload(Request $request){

        $downfile=Downfile::find($request->id);
        if(!$downfile){
            return '文件不存在！';
        }
        $path=getcwd().$downfile->url;
        down_file($path);
    }


}
